<?php

namespace App\Http\Controllers\AdminApi\User;

use App\Http\Models\Admin\SubmitReportTime;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class SystemController extends Controller
{

    public function getTime()
    {
        $reult = SubmitReportTime::get();
        return response()->json(['code' => 20000, 'message' => '获取时间成功!', 'data' => $reult]);
    }

    //设置月度报表提交时间
    public function submitTime(Request $request)
    {
        $time = $request->get('time');
        $start = $time['start'];
        $end = $time['end'];
        $count = SubmitReportTime::get();
        $submitReport = new SubmitReportTime();
        $submitReport->start = $start;
        $submitReport->end = $end;

        if ($start < 1 || $start > 31) {
            return response()->json(['code' => 20001, 'message' => '开始时间不合法，请重新输入!', 'data' => null]);
        }
        if ($end < 1 || $end > 31) {
            return response()->json(['code' => 20001, 'message' => '结束时间不合法，请重新输入!', 'data' => null]);
        }
        if ($end < $start) {
            return response()->json(['code' => 20001, 'message' => '结束时间小于开始时间，请重新输入!', 'data' => null]);
        }
        $flag = $submitReport->save();
        if ($flag) {
            if (sizeof($count) > 0) {
                SubmitReportTime::where('id', $count[0]['id'])->delete();
            }
            return response()->json(['code' => 20000, 'message' => '提交成功!', 'data' => null]);
        } else {
            return response()->json(['code' => 20001, 'message' => '提交失败!', 'data' => null]);
        }
    }
}
